package com.integrationpath.mengine.console;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.Servlet;

import org.eclipse.equinox.jsp.jasper.JspServlet;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;

import com.integrationpath.mengine.console.helper.BundleEntryHttpContext;
import com.integrationpath.mengine.console.helper.ContextPathServletAdaptor;
import com.integrationpath.mengine.console.service.PluginService;

public class Activator implements BundleActivator {

	private static final String NAME_FILTER = "com.integrationpath.mengine";
	private ServiceTracker httpServiceTracker;
	private static List bundles = new ArrayList();
	private static List services = new ArrayList();
	private static BundleContext bundleContext;
	private static ServiceTracker serviceTracker;
	
	public static BundleContext getBundleContext() {
		return bundleContext;
	}

	public static void setBundleContext(BundleContext bundleContext) {
		Activator.bundleContext = bundleContext;
	}

	public static List getServices() {
		loadServices();
		return services;
	}

	public static void setServices(List services) {
		Activator.services = services;
	}

	public static List getBundles() {
		loadBundles(getBundleContext());
		return bundles;
	}
	
	public static void setBundles(List bundles) {
		Activator.bundles = bundles;
	}

	public void start(BundleContext context) throws Exception {
		httpServiceTracker = new HttpServiceTracker(context);
		httpServiceTracker.open();
		
		serviceTracker =  new ServiceTracker(context, PluginService.class.getName(), null);
		serviceTracker.open();
		
		loadServices();
		
		loadBundles(context);
	}

	private static void loadServices() {
		List localServices = new ArrayList();
		Object[] services = (Object[])serviceTracker.getServices();
		if(services != null)
		for (Object object : services) {
			PluginService service = (PluginService)object;
			localServices.add(service);
		}
		setServices(localServices);
	}

	public static void loadBundles(BundleContext context) {
		List localBundles = new ArrayList();

		Bundle[] containerBundles = context.getBundles();
		for (int i = 0; i < containerBundles.length; i++) {
			Bundle bundle = containerBundles[i];
			if (bundle.getSymbolicName().startsWith(NAME_FILTER)) {
				BundleData bundleData = new BundleData();
				bundleData.setSymbolicName(bundle.getSymbolicName());
				bundleData.setId(bundle.getBundleId());
				bundleData.setName((String) bundle.getHeaders().get(
						"Bundle-Name"));
				bundleData.setState("" + bundle.getState());
				bundleData.setVersion((String) bundle.getHeaders().get(
						"Bundle-Version"));
				localBundles.add(bundleData);
			}
		}

		setBundles(localBundles);
		setBundleContext(context);
	}

	public void stop(BundleContext context) throws Exception {
		httpServiceTracker.close();
		serviceTracker.close();
		httpServiceTracker = null;
		bundles = null;
	}

	private class HttpServiceTracker extends ServiceTracker {

		public HttpServiceTracker(BundleContext context) {
			super(context, HttpService.class.getName(), null);
		}

		public Object addingService(ServiceReference reference) {
			HttpService httpService = (HttpService) context.getService(reference);
			try {
				HttpContext commonContext = new BundleEntryHttpContext(context.getBundle(), "/web"); //$NON-NLS-1$
				httpService.registerResources("/console", "/", commonContext); //$NON-NLS-1$ //$NON-NLS-2$
				
				Servlet adaptedJspServlet = new ContextPathServletAdaptor(new JspServlet(context.getBundle(), "/web"), "/console"); //$NON-NLS-1$//$NON-NLS-2$
				httpService.registerServlet("/console/*.jsp", adaptedJspServlet, null, commonContext); //$NON-NLS-1$
				httpService.registerServlet("/consoleServlet", new ConsoleServlet(), null, null); //$NON-NLS-1$
				// see plugin.xml for extension points declaration for servlets
				// and resources
			} catch (Exception e) {
				e.printStackTrace();
			}
			return httpService;
		}

		public void removedService(ServiceReference reference, Object service) {
			HttpService httpService = (HttpService) service;
			httpService.unregister("/consoleServlet"); //$NON-NLS-1$
			httpService.unregister("/console/*.jsp"); //$NON-NLS-1$
			super.removedService(reference, service);
		}
	}
}
